package com.example.qcweb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dao.DBDao;
import com.example.modal.MultimediaDemand;
import com.example.modal.MultimediaRecord;
import com.example.modal.WorkingProcedure;
import com.example.qc.R;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MultMediaActivity extends Activity {

    public class lvButtonClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.tvmoperator:
                    if (operator.equals(null)) {

                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        controlname = "operator";
                        TransferID();
                        StartAsignActivity();

                        new AlertDialog.Builder(MultMediaActivity.this)
                                .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                .setPositiveButton("确定", null).show();
                        // StartAsignActivity(SystemId);
                    }
                    break;

                case R.id.tvmchecker:
                    if (operator.equals(null) & rechecker.equals(null)) {

                        controlname = "rechecker";
                        TransferID();
                        StartAsignActivity();

                    } else {
                        new AlertDialog.Builder(MultMediaActivity.this)
                                .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                .setPositiveButton("确定", null).show();

                    }
                    break;

                case R.id.tvmtechnic:
                    if (checker.equals(null)) {
                        controlname = "checker";
                        TransferID();
                        StartAsignActivity();
                    } else {

                        new AlertDialog.Builder(MultMediaActivity.this)
                                .setTitle("警告提示").setMessage("请检验员刷卡取消签字")
                                .setPositiveButton("确定", null).show();

                    }

                    break;
            }
        }

    }

    private String SystemId, activityname = "MultMediaActivity";
    private String controlname = null;
    private Intent myAssiIntent;
    private String operator, rechecker, checker;

    public class MediaAdapter extends BaseAdapter {
        public class ViewHolder {
            public TextView multinumberTextView;
            public TextView posiTextView;
            public TextView photoView;
            public TextView contentView;
            public TextView photonoview;
            public TextView moperatorView;
            public TextView mcheckerView;
            public TextView mtechnicView;


        }

        LayoutInflater inflater;

        public MediaAdapter(Context context) {
            super();
            inflater = LayoutInflater.from(context);


            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != multimediaDemands) {
                return multimediaDemands.size();
            } else {
                return 0;

            }
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (arg1 == null) {
                arg1 = inflater.inflate(R.layout.activity_mediarecordcell, null);
                viewHolder = new ViewHolder();
                viewHolder.multinumberTextView = (TextView) arg1.findViewById(R.id.medianumber);
                viewHolder.posiTextView = (TextView) arg1.findViewById(R.id.tvposition);
                viewHolder.photoView = (TextView) arg1.findViewById(R.id.tvphoto);
                viewHolder.contentView = (TextView) arg1.findViewById(R.id.tvcontent);
                viewHolder.photonoview = (TextView) arg1.findViewById(R.id.tvphotono);
                viewHolder.moperatorView = (TextView) arg1.findViewById(R.id.tvmoperator);
                viewHolder.mcheckerView = (TextView) arg1.findViewById(R.id.tvmchecker);
                viewHolder.mtechnicView = (TextView) arg1.findViewById(R.id.tvmtechnic);
                arg1.setTag(viewHolder);


            } else {
                viewHolder = (ViewHolder) arg1.getTag();

            }
            MultimediaRecord multimediaRecord = multimediarecords.get(arg0);
            MultimediaDemand multimediaDemand = multimediaDemands.get(arg0);
            String idString = multimediaDemand.getSystemId();
            //序号
            viewHolder.multinumberTextView.setText(idString);
            //部位
            viewHolder.posiTextView.setText(multimediaDemand.getProductPosition());
            //照相/摄像
            String strType = null;
            if (multimediaDemand.getMultimediaType() == "1") {
                strType = "照相";

            } else if (multimediaDemand.getMultimediaType() == "2") {
                strType = "摄像";

            }
            viewHolder.photoView.setText(strType);
            //内容
            viewHolder.contentView.setText(multimediaDemand.getRecordContent());
            //照片编号
            viewHolder.photonoview.setText(multimediaRecord.getMultimediaNumber());
            //操作者
            viewHolder.moperatorView.setText(multimediaRecord.getRecorder());
            //检验员
            viewHolder.mcheckerView.setText(multimediaRecord.getVerifier());
            //工艺员
            viewHolder.mtechnicView.setText(multimediaRecord.getTechnic());
            // 给参数赋值
            SystemId = multimediaDemand.getSystemId();
            operator = multimediaRecord.getRecorder();
            rechecker = multimediaRecord.getVerifier();
            checker = multimediaRecord.getTechnic();
            viewHolder.moperatorView.setOnClickListener(new lvButtonClick());
            viewHolder.mcheckerView.setOnClickListener(new lvButtonClick());
            viewHolder.mtechnicView.setOnClickListener(new lvButtonClick());
            return arg1;
        }

    }

    WorkingProcedure wpProcedure;
    Drawable bgDrawable;
    List<MultimediaRecord> multimediarecords = new ArrayList<MultimediaRecord>();
    List<MultimediaDemand> multimediaDemands = new ArrayList<MultimediaDemand>();
    Bitmap btpBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mediarecord);
        // bgDrawable=getResources().getDrawable(R.drawable.bgpartexammdedia);
        // LinearLayout layout1=(LinearLayout)findViewById(R.id.Layout1);
        // layout1.setBackgroundDrawable(bgDrawable);
        /*
		 * btpBitmap=readBitmap(MultMediaActivity.this, R.drawable.bgmultmedia);
		 * LinearLayout layout1=(LinearLayout)findViewById(R.id.Layout1);
		 * layout1.setBackground((Drawable)btpBitmap);
		 */
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        wpProcedure = (WorkingProcedure) bundle.getSerializable("key");
        SystemId = wpProcedure.getSystemId();
        // setContentView(R.layout.)
        findViews();
        getData();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        bgDrawable = null;

    }

    public static Bitmap readBitmap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    private void getData() {
        // TODO Auto-generated method stub
        multimediarecords = DBDao.findMultimediaRecords(wpProcedure.getKEYCardCode(), wpProcedure.getSystemId());
        multimediaDemands = DBDao.findMultimediaDemands(wpProcedure.getKEYCardCode(), wpProcedure.getSystemId());
    }

    private void findViews() {
        // TODO Auto-generated method stub
        ListView lvListView = (ListView) findViewById(R.id.medialv);
        MediaAdapter adapter = new MediaAdapter(MultMediaActivity.this);
        lvListView.setAdapter(adapter);
        TextView tView = (TextView) findViewById(R.id.mediatext);
        tView.setText(MyGlobal.KeycardCode + "-工序" + wpProcedure.getProduce()
                + "-工步" + wpProcedure.getStep() + "多媒体信息表");
        ImageButton multButton = (ImageButton) findViewById(R.id.multibtn);
        multButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MultMediaActivity.this, OperateActivity.class);
                intent.putExtra("type", "MultiItem");
                intent.putExtra("key1", wpProcedure);
                startActivity(intent);
            }
        });
    }

    private void TransferID() {
        // TODO Auto-generated method stub
        myAssiIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("systemid", SystemId);
        bundle.putString("activityname", activityname);
        bundle.putString("controlname", controlname);
        myAssiIntent.putExtras(bundle);
        Log.i("tag", controlname);
    }

    private void StartAsignActivity() {
		/* 把bundle对象assign给Intent */
        // Intent asignIntent = new Intent();
        // SystemId=
        myAssiIntent.setClass(MultMediaActivity.this, signactivity.class);
        startActivity(myAssiIntent);

    }

}
